import java.sql.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class TestHeap {
    private int[] elem;
    private int usedSize;
    public TestHeap(){
        this.elem = new int[10];
    }

    public void initHeap(int[] array){
        for (int i = 0; i < array.length;i++) {
            elem[i] = array[i];
            usedSize++;
        }
    }
    public void creatHeap(){
        for (int parent = (usedSize-1-1)/2; parent >= 0 ; parent--) {
            shiftDown(parent,10);
        }
    }

    private void shiftDown(int parent, int usedSize) {
        int child = 2*parent +1;//左孩子的下标
        while (child < usedSize){
            if (child+1 < usedSize && elem[child] < elem[child+1]){
                child++;
            }
            //child一定是左右孩子最大值的下标
            if (elem[child] > elem[parent]){
                swap(child,parent);
                parent = child;
                child = 2*parent +1;
            }else{
                //已经是大根堆了
                break;
            }
        }
    }
    private void swap(int i,int j){
        int tmp = elem[i];
        elem[i] = elem[j];
        elem[j] = tmp;
    }


    //插入元素
    public void offer(int val){
        if (isFull()){
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        this.elem[usedSize] = val;
        shiftUp(usedSize);
        usedSize++;
    }
    //向上调整
    private void shiftUp(int child) {
        int parent = (child-1)/2;
        while (child > 0){
            if (elem[child] > elem[parent]){
                swap(child,parent);
                child = parent;
                parent = (child-1)/2;
            }else {
                break;
            }
        }
    }
    private boolean isFull(){
        return usedSize == elem.length;
    }


    //删除操作
    public int poll(){
        int tmp = elem[0];
        swap(0,usedSize-1);
        usedSize--;
        shiftDown(0,usedSize);
        return tmp;
    }


    //top-k问题求最大
    public int[] maxlestK(int[] array,int k){
        int[] ret = new int[k];
        if (array == null|| k <= 0){
            return ret;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(array[i]);
        }
        for (int i =k; i < array.length; i++) {
            int top = priorityQueue.peek();
            if (array[i] > top){
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }


    //top-k问题求最小
    class IntCmp implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    }
    public int[] smallestK(int[] array,int k){
        int[] ret = new int[k];
        if (array == null|| k <= 0){
            return ret;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new IntCmp());

        for (int i = 0; i < k; i++) {
            priorityQueue.offer(array[i]);
        }
        for (int i =k; i < array.length; i++) {
            int top = priorityQueue.peek();
            if (array[i] < top){
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }
}
